package com.example.test9_3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.select_user_icon);//获取"选择头像”按钮
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, HeadActivity.class);
                startActivityForResult(intent, 0x11);//启动指定的Activity
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x11 && resultCode == 0x12) {//判断是否为待处理的结果
            Bundle bundle = data.getExtras();//获取传递的数据包
            int imageId = bundle.getInt("imageId");//获取选择的头像id
            ImageView iv = (ImageView) findViewById(R.id.user_icon);//获取布局文件中添加的ImageView组件
            iv.setImageResource(imageId);//显示选择的头像
        }
    }
}